/*=============================================================================

  CAM CONTROL CONFIGURATION

  FILE: HCAM_CFG.c
  
  DESCRIPTION

    CAM Control configuration
    
  EXTERNALIZED FUNCTIONS


   Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who       what, where, why
  ---------  -------  -------------------------------------------------------
   07/15/04   nso4th    Initial version

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include "HCAM_cfg.h"
#include <stdio.h>
#include <string.h>

/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/
#define CAM_MOD_NUM		(1)
#define XCL_U1000C			(0)
 
/*-------------------------------------------------------------------
  DEVICE CONFIGURATION
-------------------------------------------------------------------*/
// devName, comPort, subID, baudRate, waitTime
static cam_setup_s	gCAMDev_0 = { XCL_U1000C, 1, CAM_38400_BRATE, 6000 };

/*=============================================================================
  EXTERNALIZED FUNCION DEFINITION
=============================================================================*/
cam_cfg_s *cam_default_alloc(void)
{
	cam_cfg_s		*cfg;

	cfg = cam_cfg_alloc();

	cfg->dev_num = CAM_MOD_NUM;
	cfg->set = (cam_setup_s*)memCalloc(sizeof(*(cfg->set))*(cfg->dev_num));

	memCopy(cfg->set, &gCAMDev_0, sizeof(gCAMDev_0));
	
	return	cfg;
}

void cam_default_free(cam_cfg_s *cfg)
{
	if(cfg == NULL)
		return;

	if(cfg->set)
	{
		memFree(cfg->set);
		cfg->set = NULL;
	}
	cam_cfg_free(cfg);
}

/*=============================================================================
  CONSTATN DEFINITION
=============================================================================*/
#define HCFG_HEADER_LEN				(9)
#define HCFG_HEADER					("CAM")
#define HCAM_VER_MAJOR				(01)
#define HCAM_VER_MINOR				(00)

/*=============================================================================
  STRUCTURE DEFINITION
=============================================================================*/
typedef struct
{
	char				head[HCFG_HEADER_LEN];  // "HFGV01.00"
	cam_cfg_s		*cfg;
	word			wsum;  // Check sum
} cam_cfg_file_s;

/*=============================================================================
  EXTERNALIZED FUNCTION DEFINITION
=============================================================================*/
cam_cfg_s *cam_cfg_alloc(void)
{
	cam_cfg_s		*cfg;

	cfg = (cam_cfg_s*)memCalloc(sizeof(*cfg));

	return	cfg;
}

void cam_cfg_free(cam_cfg_s *cfg)
{
	if(cfg == NULL)
		return;

	if(cfg->set)
	{
		memFree(cfg->set);
	}
	memFree(cfg);
}

cam_cfg_s *cam_cfg_load(char *pszname)
{
	cam_cfg_s			*cfg;
	mcxt				pFile;
	cam_cfg_file_s		hf;
	byte					*buf, *p;
	int32				nLen, lr, i;
	word				wsum;
	char					head[HCFG_HEADER_LEN+1];

	
	if((pFile = FileIo_Open(pszname, FIO_OPEN_R)) == NULL)
		return	NULL;

	if((FileIo_Size(pFile, (dword*)&nLen) == FALSE) || (nLen < (HCFG_HEADER_LEN+2+4)))
	{
		FileIo_Close(pFile);
		return	NULL;
	}

	// Read
	buf = (byte*)memCalloc(nLen);
	if((FileIo_Read(pFile, buf, nLen, (dword*)&lr) == FALSE) || (nLen != lr))
	{
		memFree(buf);
		FileIo_Close(pFile);
		return	NULL;
	}
	FileIo_Close(pFile);

	// Compare check-sum
	p = buf + nLen - 2;
	wsum = FileIo_CheckSum((word*)buf, (nLen-2));
	if(wsum != *((word*)p))
	{
		memFree(buf);
		return	NULL;
	}

	// Read Header & Check Version
	p = buf;
	memCopy(hf.head, p, sizeof(hf.head));
	p += sizeof(hf.head);
	sprintf(head,"%sV%002d.%002d", HCFG_HEADER, HCAM_VER_MAJOR, HCAM_VER_MINOR);
	if(strncmp(hf.head, head, HCFG_HEADER_LEN) != 0)
	{
		memFree(buf);
		return	NULL;
	}

	// Get Link Information
	cfg = (cam_cfg_s*)memCalloc(sizeof(*cfg));
	cfg->dev_num = *((int32*)p);
	p += sizeof(cfg->dev_num);

	cfg->set = (cam_setup_s*)memCalloc(sizeof(*(cfg->set))*(cfg->dev_num));
	for(i=0; i<cfg->dev_num; i++)
	{
		memCopy(cfg->set+i, p, sizeof(*(cfg->set)));
		p += sizeof(*(cfg->set));
	}
	memFree(buf);
		
	return	cfg;
}

boolean cam_cfg_save(cam_cfg_s *cfg, char *pszname)
{
	mcxt		pFile;
	byte			*buf, *p;
	int32		nLen, lw, i;
	char			head[HCFG_HEADER_LEN+1];
	
	if((cfg == NULL) || (cfg->dev_num == 0))
		return	FALSE;

	if((pFile = FileIo_Open(pszname, FIO_OPEN_WR)) == NULL)
		return	NULL;

	// Total Length
	nLen = HCFG_HEADER_LEN + sizeof(cfg->dev_num) + (cfg->dev_num)*(sizeof(*(cfg->set))) + 2;
	buf = (byte*)memCalloc(nLen);
	p = buf;
	
	// Make Header
	sprintf(head,"%sV%002d.%002d", HCFG_HEADER, HCAM_VER_MAJOR, HCAM_VER_MINOR);
	memCopy(p, head, HCFG_HEADER_LEN);
	p += HCFG_HEADER_LEN;
	
	// Config. Information
	memCopy(p, cfg, sizeof(cfg->dev_num));
	p += sizeof(cfg->dev_num);
	for(i=0; i<cfg->dev_num; i++)
	{
		memCopy(p, (cfg->set + i), sizeof(*(cfg->set)));
		p += sizeof(*(cfg->set));
	}

	// Get Check sum
	*((word*)(p)) = FileIo_CheckSum((word*)buf, nLen-2);

	// Save File
	if((FileIo_Write(pFile, buf, nLen, (dword*)&lw) == FALSE) || (nLen != lw))
	{
		memFree(buf);
		FileIo_Close(pFile);
		FileIo_DeleteFile(pszname);

		return	FALSE;
	}

	memFree(buf);
	FileIo_Close(pFile);
	
	return	TRUE;
}

/*--- END OF HOSC2_cfg.c ---*/
